package com.chb.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chb.system.mapper.UserMapper;
import com.chb.system.service.UserService;
import com.chb.system.vo.UserAndDeptVo;
import com.chb.system.vo.UserVo;
import com.hos.entity.sys.Role;
import com.hos.entity.sys.User;
import com.hos.utils.MybatisPlusUtils;
import com.hos.vo.Result;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author: 陈海彬
 * @date: Created in 2023/12/7 19:36
 * @description:
 * @modified By:
 * @version:
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public User getUserById(Long id) {
        User user = userMapper.selectById(id);
        return user;
    }

    @Override
    public User findUserByPhone(String phone) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone, phone);
        wrapper.eq(User::getStatus, 0);
        // wrapper.eq(User::getDelFlag, 0);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public Page<User> findUserByCond(UserVo userVo) {
        Page<User> page = new Page<>(userVo.getCurrent(), userVo.getPageSize());
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(userVo.getDeptId())) {
            wrapper.eq(User::getDeptId, userVo.getDeptId());
        }
        if (StringUtils.hasText(userVo.getUserName())) {
            wrapper.like(User::getUserName, userVo.getUserName());
        }
        if (StringUtils.hasText(userVo.getPhone())) {
            wrapper.like(User::getPhone, userVo.getPhone());
        }
        if (Objects.nonNull(userVo.getDateRange())) {
            wrapper.between(User::getCreateTime, userVo.getDateRange()[0], userVo.getDateRange()[1]);
        }
        if (StringUtils.hasText(userVo.getStatus())) {
            wrapper.eq(User::getStatus, userVo.getStatus());
        }
        // wrapper.eq(User::getDelFlag, 0);
        return userMapper.selectPage(page, wrapper);
    }

    @Override
    public List<User> findSchedulingUser() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("scheduling_flag", 0);
        // wrapper.eq("del_flag", 0);
        List<User> users = userMapper.selectList(wrapper);
        return users;
    }

    @Override
    public List<User> findUser(Map<String, Object> params) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("scheduling_flag", 0);
        // wrapper.eq("del_flag", 0);
        if (Objects.nonNull(params.get("userId"))) {
            wrapper.eq("user_id", params.get("userId"));
        }
        if (Objects.nonNull(params.get("deptId"))) {
            wrapper.eq("dept_id", params.get("deptId"));
        }
        List<User> users = userMapper.selectList(wrapper);
        return users;
    }


    @Override
    public Integer addUser(User user) {
        // 拿个当前的时间
        user.setCreateTime(new Date());
        user.setPassword("$2a$10$cI7e7bgSs9.9nNHhxKO9LuK/Ll.AeZwgUyZb77oD2y3UwwZyZhWG6");
        return userMapper.insert(user);
    }

    @Override
    public List<User> getUserByParams(Map<String, Object> params) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(User::getSchedulingFlag, 0);
        // wrapper.eq(User::getDelFlag, 0);
        // 1.进行条件查询
        if (Objects.nonNull(params.get("userId"))) {
            wrapper.eq(User::getUserId, params.get("userId"));
        }
        if (Objects.nonNull(params.get("deptId"))) {
            wrapper.eq(User::getDeptId, params.get("deptId"));
        }

        return userMapper.selectList(wrapper);
    }

    @Override
    public Integer deleteUserByIds(List<Long> ids) {
        Integer i = userMapper.deleteBatchIds(ids);
        return i;
    }

    @Override
    public Integer updateUser(User user) {

        int i = userMapper.updateById(user);
        return i;
    }

    /**
     * 查询所有的可用用户
     *
     * @return
     */
    @Override
    public List<User> selectAllUser() {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getStatus, 0);
        return userMapper.selectList(wrapper);
    }

    /**
     * 根据科室id查询医生列表信息
     *
     * @param deptId 科室id
     */
    @Override
    public List<UserAndDeptVo> getUserListByDeptId(Long deptId) {
        return this.userMapper.userList(deptId);
    }

    @Override
    public Integer resetPwd(List<Long> userIds, String newPassWord) {
        List<User> users = this.userMapper.selectBatchIds(userIds);
        for (User user : users) {
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String encode = encoder.encode(newPassWord);
            user.setPassword(encode);
            userMapper.updateById(user);
        }
        return null;
    }

    /**
     * 根据科室IDS查询用户
     *
     * @param deptids 科室IDS
     * @return
     */
    @Override
    public boolean getUserByDeptIds(List<Long> deptids) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(User::getDeptId, deptids);
        int size = userMapper.selectList(wrapper).size();
        if (size > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<User> getUserListRanges() {
        return MybatisPlusUtils.getAny(userMapper, null, 4L);
    }

    @Override
    public Result<String> userPhone(String phone) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone, phone);
        User user = this.userMapper.selectOne(wrapper);
        if (user != null) {
            return new Result<>(208, "手机号重复", null);
        }
        return new Result<>(200, "手机号可用", null);
    }

}